package com.iceclean.serviceImpl;

import com.iceclean.dao.ResourceDao;
import com.iceclean.daoImpl.ResourceDaoImpl;
import com.iceclean.po.CommentResource;
import com.iceclean.service.ResourceService;
import com.iceclean.util.MyLogger;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author : Ice'Clean
 * @date : 2021-05-15
 */
public class ResourceServiceImpl implements ResourceService {
    ResourceDao resourceDao = new ResourceDaoImpl();
    MyLogger myLogger = new MyLogger("service");

    @Override
    public CommentResource findCommentResource(int resourceId) {
        return resourceDao.findCommentResource(resourceId);
    }

    @Override
    public CommentResource createCommentResource(String resourceIdentify, String resourceName) {
        int id = resourceDao.createCommentResource(new CommentResource(resourceIdentify, resourceName));
        return resourceDao.findCommentResource(id);
    }

    @Override
    public void checkContentResource(String commentContent, String resourceIdentify, int commentId) {
        // 首先抽离处内容中的图片资源
        List<String> srcList = new ArrayList<>();
        String[] tempSrc = commentContent.split("src=\"/QG_PostBar/post_comment_Image/");
        for (int i = 1; i < tempSrc.length ; i++) {
            String[] src2 = tempSrc[i].split("\"");
            srcList.add(src2[0]);
        }

        // 从数据库中获取图片资源
        List<CommentResource> commentResourceList = resourceDao.findAllCommentResourceByIdentify(resourceIdentify);

        myLogger.writeInfo("唯一标识：" + resourceIdentify);
        myLogger.writeInfo("获取到的数据库记录" + Arrays.toString(commentResourceList.toArray()));

        // 有上传图片资源的话，进行检查，删除掉已废弃的图片资源
        if (commentResourceList.size() > 0) {
            // 存放存在标识
            boolean isExist = false;

            myLogger.writeInfo("有图片资源，开始删除, 要删除的长度：" + commentResourceList.size());

            // 将图片资源取出来做对比
            for (CommentResource tempResource : commentResourceList) {
                // 遍历比较内容中的图片资源
                for(String name : srcList) {
                    isExist = name.equals(tempResource.getResourceName());
                    if(isExist) {
                        break;
                    }
                }

                // 不存在则将该资源从服务器删去，并删除该记录
                if (!isExist) {
                    deleteResource(tempResource);
                } else {
                    // 存在的话则为资源绑定其对应的评论id
                    tempResource.setCommentId(commentId);
                    resourceDao.updateCommentResource(tempResource);
                }

                isExist = false;
            }
        } else {
            myLogger.writeInfo("数据库中并没有图片资源可以删除");
        }
    }

    @Override
    public void deleteResource(CommentResource commentResource) {
        if(!new File("D:/1_IceCleanDevelop/2_Java/1_Java_Practices/3 ~ JavaWeb/QG_Tieba/out/artifacts/QG_Tieba_war_exploded/post_comment_Image/" + commentResource.getResourceName()).delete()) {
            myLogger.writeInfo("从服务器中删除图片失败！");
        } else {
            myLogger.writeInfo("成功从服务器中删除图片");
        }

        // 从数据库中删除该记录
        if(!resourceDao.deleteCommentResource(commentResource.getResourceId())){
            myLogger.writeInfo("从数据库中删除图片失败！");
        } else {
            myLogger.writeInfo("成功从数据库中删除图片");
        }
    }

    @Override
    public void deleteResourceByComment(int commentId) {
        // 首先找到该评论所有的资源记录
        List<CommentResource> commentResourceList = resourceDao.findResourceByCommentId(commentId);

        // 在服务器中删去对应资源，然后销毁记录本身
        for(CommentResource tempResource : commentResourceList) {
            deleteResource(tempResource);
        }
    }
}
